package com.pig4cloud.pig.homecenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import com.pig4cloud.pig.homecenter.entity.HassEntityRegistryEntity;
import com.pig4cloud.pig.homecenter.service.HassEntityRegistryService;
import com.pig4cloud.pig.homecenter.vo.CallServiceVO;
import com.pig4cloud.pig.homecenter.ws.HassWebsocketMessageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/hassService/call" )
@Tag(description = "HassService" , name = "hass服务调用" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HassServiceController {

	private final HassEntityRegistryService hassEntityRegistryService;

	@Operation(summary = "发送打开或关闭指令", description = "发送打开或关闭指令")
	@PostMapping("/onOff")
	public R callOnOffService(@RequestBody CallServiceVO params) {

		//区域操作
		if(!CollectionUtils.isEmpty(params.getAreaIds())){
			if(CollectionUtils.isEmpty(params.getDomains())){
				return R.failed("区域操作时请指定实体类型");
			}
			List<String> domains = new ArrayList<>();
			params.getDomains().forEach(domain -> {
				switch (domain) {
					case "light":
					case "switch":
					case "fan":
					case "humidifier":
					case "climate":
						domains.add(domain);
						break;
					default:
						log.warn("暂不支持的实体类型:{}",domain);
						break;
				}
			});

			QueryWrapper<HassEntityRegistryEntity> areaQueryWrapper = new QueryWrapper<>();
			areaQueryWrapper.lambda().eq(HassEntityRegistryEntity::getUserId, SecurityUtils.getUser().getId())
					.in(HassEntityRegistryEntity::getAreaId,params.getAreaIds())
					.in(HassEntityRegistryEntity::getShowAs,domains)
					.eq(HassEntityRegistryEntity::getVisible,true);
			List<HassEntityRegistryEntity> entityList = hassEntityRegistryService.list(areaQueryWrapper);
			entityList.forEach(entity -> {
				Map<String,Object> serviceData = new HashMap<>();
				serviceData.put("entity_id", entity.getEntityId());
				HassWebsocketMessageUtils.callService(entity.getEntityType(), params.getOperation().name(),serviceData);
			});
			return R.ok();

		}

		//实体操作
		if(!CollectionUtils.isEmpty(params.getEntityIds())){
			QueryWrapper<HassEntityRegistryEntity> queryWrapper = new QueryWrapper<>();
			queryWrapper.lambda().eq(HassEntityRegistryEntity::getUserId, SecurityUtils.getUser().getId())
					.in(HassEntityRegistryEntity::getEntityId,params.getEntityIds())
					.eq(HassEntityRegistryEntity::getVisible,true);
			List<HassEntityRegistryEntity> entityList = hassEntityRegistryService.list(queryWrapper);
			entityList.forEach(entity -> {
				switch (entity.getEntityType()) {
					case "light":
					case "switch":
					case "fan":
					case "humidifier":
					case "climate":
						Map<String,Object> serviceData = new HashMap<>();
						serviceData.put("entity_id", entity.getEntityId());
						HassWebsocketMessageUtils.callService(entity.getEntityType(), params.getOperation().name(),serviceData);
						break;
					default:
						log.warn("暂不支持的实体类型:{},实体名:{}",entity.getEntityType(),entity.getCustomeName());
						break;
				}
			});
		}

		return R.ok();
	}


}
